﻿using System;
using System.IO.IsolatedStorage;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Xml.Linq;
using Coding4Fun.Phone.Controls;
using Microsoft.Phone.Controls;
using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.Serialization.Json;
using System.Windows.Navigation;
using System.Windows.Media.Imaging;
using Facebook;


namespace Android_Web_Browser
{
    public partial class MainPage : PhoneApplicationPage
    {
        #region Facebook
        private const string AppId = "143287969161537";

        /// <summary>
        /// Extended permissions is a comma separated list of permissions to ask the user.
        /// </summary>
        /// <remarks>
        /// For extensive list of available extended permissions refer to 
        /// https://developers.facebook.com/docs/reference/api/permissions/
        /// </remarks>
        private const string ExtendedPermissions = "user_about_me,read_stream,publish_stream";

        private readonly FacebookClient _fb = new FacebookClient();

        private string _accessToken;
        private string _userId;

        #endregion

        string useragent = "User-Agent: Mozilla/5.0 (Linux; Android 4.0.4; LG-L160L Build/IML74K) AppleWebkit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Mobile Safari/535.19";

        //WebClient pu;

        string iscb1checked;
        string isrb1checked;
        string isrb2checked;
        string isrb3checked;
        string ists1checked;

        public string URL { get; set; }
        public string SearchBoxText { get; set; }

        bool ismenuopen = false;

        // Constructor
        public MainPage()
        {
            InitializeComponent();

            wb1.IsEnabled = true;
            wb2.IsEnabled = false;
            wb3.IsEnabled = false;
            wb4.IsEnabled = false;
            wb5.IsEnabled = false;
            wb6.IsEnabled = false;
            wb7.IsEnabled = false;
            wb8.IsEnabled = false;


            wb1.Visibility = Visibility.Visible;
            wb2.Visibility = Visibility.Collapsed;
            wb3.Visibility = Visibility.Collapsed;
            wb4.Visibility = Visibility.Collapsed;
            wb5.Visibility = Visibility.Collapsed;
            wb6.Visibility = Visibility.Collapsed;
            wb7.Visibility = Visibility.Collapsed;
            wb8.Visibility = Visibility.Collapsed;

            

            #region Load Settings
            if (IsolatedStorageSettings.ApplicationSettings.Contains("DataKey"))
            {
                string datakey = IsolatedStorageSettings.ApplicationSettings["DataKey"] as string;

                
                if (datakey.Contains("ts1ischecked"))
                {
                    TS1.IsChecked = true;
                }
                else
                {
                    TS1.IsChecked = false;
                }

                if (datakey.Contains("rb1ischecked"))
                {
                    RB1.IsChecked = true;
                }
                else
                {
                    RB1.IsChecked = false;
                }

                if (datakey.Contains("rb2ischecked"))
                {
                    RB2.IsChecked = true;
                }
                else
                {
                    RB2.IsChecked = false;
                }

                if (datakey.Contains("rb3ischecked"))
                {
                    RB3.IsChecked = true;
                }
                else
                {
                    RB3.IsChecked = false;
                }

                if (datakey.Contains("cb1ischecked"))
                {
                    CB1.IsChecked = true;
                    BrowserHost.Margin = new Thickness(0, 60, 0, 60);
                    panorama1.Visibility = Visibility.Visible;

                }
                else
                {
                    CB1.IsChecked = false;
                    BrowserHost.Margin = new Thickness(0, 0, 0, 60);
                    panorama1.Visibility = Visibility.Collapsed;
                }
            }
            else
            {
                //Do nothing
            }

            #endregion

            //Bing picture of the day
            //pu = new WebClient();

            //string pp = "http://www.bing.com/HPImageArchive.aspx?format=xml&idx=0&n=1";
            
            //pu.DownloadStringAsync(new Uri(pp));
            
            //pu.DownloadStringCompleted += new DownloadStringCompletedEventHandler(pun_DownloadStringCompleted);

        }

        //Updates DataKey
        void UpdateDataKey()
        {
            try
            {
                if (TS1.IsChecked == true)
                {
                    ists1checked = "ts1ischecked";
                }
                else
                {
                    ists1checked = "no";
                }

                if (CB1.IsChecked == true)
                {
                    iscb1checked = "cb1ischecked";
                }
                else
                {
                    iscb1checked = "no";
                }


                if (RB1.IsChecked == true)
                {
                    isrb1checked = "rb1ischecked";
                }
                else
                {
                    isrb1checked = "no";
                }

                if (RB2.IsChecked == true)
                {
                    isrb2checked = "rb2ischecked";
                }
                else
                {
                    isrb2checked = "no";
                }

                if (RB3.IsChecked == true)
                {
                    isrb3checked = "rb3ischecked";
                }
                else
                {
                    isrb3checked = "no";
                }

                IsolatedStorageSettings ISSetting = IsolatedStorageSettings.ApplicationSettings;

                if (!ISSetting.Contains("DataKey"))
                {
                    ISSetting.Add("DataKey", iscb1checked + isrb1checked + isrb2checked + isrb3checked + ists1checked);
                }
                else
                {
                    ISSetting["DataKey"] = iscb1checked + isrb1checked + isrb2checked + isrb3checked + ists1checked;
                }
                ISSetting.Save();
            }
            catch { }
        }

        //Fade in/out animation
        private Storyboard CreateFadeInOutAnimation(double from, double to, DependencyObject target, string targetProperty)
        {
            Storyboard sb = new Storyboard();
            DoubleAnimation fadeInAnimation = new DoubleAnimation();
            fadeInAnimation.From = from;
            fadeInAnimation.To = to;
            fadeInAnimation.Duration = new Duration(TimeSpan.FromSeconds(0.25));

            Storyboard.SetTarget(fadeInAnimation, target);
            Storyboard.SetTargetProperty(fadeInAnimation, new PropertyPath(targetProperty));

            sb.Children.Add(fadeInAnimation);

            return sb;
        }

        //Exit exception
        public class ExitException : Exception { }

        //Back button
        protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
        {
            e.Cancel = true;

                if (ismenuopen == true)
                {
                    CloseMenu.Begin();
                    ismenuopen = false;
                }
                else
                {
                    //Crash the app
                    throw new ExitException();
                }
        }

        //Refresh
        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            if (wb1.IsEnabled)
            {
                wb1.Navigate(new Uri(wb1.Source.AbsoluteUri, UriKind.Absolute), null, useragent);
            }

            if (wb2.IsEnabled)
            {
                wb2.Navigate(new Uri(wb2.Source.AbsoluteUri, UriKind.Absolute), null, useragent);
            }

            if (wb3.IsEnabled)
            {
                wb3.Navigate(new Uri(wb3.Source.AbsoluteUri, UriKind.Absolute), null, useragent);
            }

            if (wb4.IsEnabled)
            {
                wb4.Navigate(new Uri(wb4.Source.AbsoluteUri, UriKind.Absolute), null, useragent);
            }

            if (wb5.IsEnabled)
            {
                wb5.Navigate(new Uri(wb5.Source.AbsoluteUri, UriKind.Absolute), null, useragent);
            }

            if (wb6.IsEnabled)
            {
                wb6.Navigate(new Uri(wb6.Source.AbsoluteUri, UriKind.Absolute), null, useragent);
            }

            if (wb7.IsEnabled)
            {
                wb7.Navigate(new Uri(wb7.Source.AbsoluteUri, UriKind.Absolute), null, useragent);
            }

            if (wb8.IsEnabled)
            {
                wb8.Navigate(new Uri(wb8.Source.AbsoluteUri, UriKind.Absolute), null, useragent);
            }
        }

        //Key pressed
        private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                PB1.IsIndeterminate = true;

                if (textBox1.Text.Contains(" ") || !textBox1.Text.Contains("."))
                {
                    //Search Google

                    string String = textBox1.Text.Replace(" ", "+");

                    if (wb1.IsEnabled)
                    {
                        wb1.Navigate(new Uri("http://www.google.com/search?&q=" + String, UriKind.Absolute), null, useragent);
                    }

                    if (wb2.IsEnabled)
                    {
                        wb2.Navigate(new Uri("http://www.google.com/search?&q=" + String, UriKind.Absolute), null, useragent);
                    }

                    if (wb3.IsEnabled)
                    {
                        wb3.Navigate(new Uri("http://www.google.com/search?&q=" + String, UriKind.Absolute), null, useragent);
                    }

                    if (wb4.IsEnabled)
                    {
                        wb4.Navigate(new Uri("http://www.google.com/search?&q=" + String, UriKind.Absolute), null, useragent);
                    }

                    if (wb5.IsEnabled)
                    {
                        wb5.Navigate(new Uri("http://www.google.com/search?&q=" + String, UriKind.Absolute), null, useragent);
                    }

                    if (wb6.IsEnabled)
                    {
                        wb6.Navigate(new Uri("http://www.google.com/search?&q=" + String, UriKind.Absolute), null, useragent);
                    }

                    if (wb7.IsEnabled)
                    {
                        wb7.Navigate(new Uri("http://www.google.com/search?&q=" + String, UriKind.Absolute), null, useragent);
                    }

                    if (wb8.IsEnabled)
                    {
                        wb8.Navigate(new Uri("http://www.google.com/search?&q=" + String, UriKind.Absolute), null, useragent);
                    }
                }
                else if (textBox1.Text.StartsWith("http://") || textBox1.Text.StartsWith("www.") || textBox1.Text.Contains("."))
                {
                    //Navigate with absolute uri

                    try
                    {
                        if (wb1.IsEnabled)
                        {
                            wb1.Navigate(new Uri(textBox1.Text, UriKind.Absolute), null, useragent);
                        }

                        if (wb2.IsEnabled)
                        {
                            wb2.Navigate(new Uri(textBox1.Text, UriKind.Absolute), null, useragent);
                        }

                        if (wb3.IsEnabled)
                        {
                            wb3.Navigate(new Uri(textBox1.Text, UriKind.Absolute), null, useragent);
                        }

                        if (wb4.IsEnabled)
                        {
                            wb4.Navigate(new Uri(textBox1.Text, UriKind.Absolute), null, useragent);
                        }

                        if (wb5.IsEnabled)
                        {
                            wb5.Navigate(new Uri(textBox1.Text, UriKind.Absolute), null, useragent);
                        }

                        if (wb6.IsEnabled)
                        {
                            wb6.Navigate(new Uri(textBox1.Text, UriKind.Absolute), null, useragent);
                        }

                        if (wb7.IsEnabled)
                        {
                            wb7.Navigate(new Uri(textBox1.Text, UriKind.Absolute), null, useragent);
                        }

                        if (wb8.IsEnabled)
                        {
                            wb8.Navigate(new Uri(textBox1.Text, UriKind.Absolute), null, useragent);
                        }
                    }
                    catch
                    {
                        try
                        {
                            textBox1.Text = "http://" + textBox1.Text;

                            if (wb1.IsEnabled)
                            {
                                wb1.Navigate(new Uri(textBox1.Text, UriKind.Absolute), null, useragent);
                            }

                            if (wb2.IsEnabled)
                            {
                                wb2.Navigate(new Uri(textBox1.Text, UriKind.Absolute), null, useragent);
                            }

                            if (wb3.IsEnabled)
                            {
                                wb3.Navigate(new Uri(textBox1.Text, UriKind.Absolute), null, useragent);
                            }

                            if (wb4.IsEnabled)
                            {
                                wb4.Navigate(new Uri(textBox1.Text, UriKind.Absolute), null, useragent);
                            }

                            if (wb5.IsEnabled)
                            {
                                wb5.Navigate(new Uri(textBox1.Text, UriKind.Absolute), null, useragent);
                            }

                            if (wb6.IsEnabled)
                            {
                                wb6.Navigate(new Uri(textBox1.Text, UriKind.Absolute), null, useragent);
                            }

                            if (wb7.IsEnabled)
                            {
                                wb7.Navigate(new Uri(textBox1.Text, UriKind.Absolute), null, useragent);
                            }

                            if (wb8.IsEnabled)
                            {
                                wb8.Navigate(new Uri(textBox1.Text, UriKind.Absolute), null, useragent);
                            }
                        }
                        catch
                        {
                            MessageBox.Show("Invalid URL");
                        }
                    }
                }
            }
            //        else if (!textBox1.Text.StartsWith("https://"))
            //        {
            //            textBox1.Text = "http://" + textBox1.Text;

            //            if (wb1.IsEnabled)
            //            {
            //                wb1.Navigate(new Uri(textBox1.Text, UriKind.Absolute), null, useragent);
            //            }

            //            if (wb2.IsEnabled)
            //            {
            //                wb2.Navigate(new Uri(textBox1.Text, UriKind.Absolute), null, useragent);
            //            }

            //            if (wb3.IsEnabled)
            //            {
            //                wb3.Navigate(new Uri(textBox1.Text, UriKind.Absolute), null, useragent);
            //            }

            //            if (wb4.IsEnabled)
            //            {
            //                wb4.Navigate(new Uri(textBox1.Text, UriKind.Absolute), null, useragent);
            //            }

            //            if (wb5.IsEnabled)
            //            {
            //                wb5.Navigate(new Uri(textBox1.Text, UriKind.Absolute), null, useragent);
            //            }

            //            if (wb6.IsEnabled)
            //            {
            //                wb6.Navigate(new Uri(textBox1.Text, UriKind.Absolute), null, useragent);
            //            }

            //            if (wb7.IsEnabled)
            //            {
            //                wb7.Navigate(new Uri(textBox1.Text, UriKind.Absolute), null, useragent);
            //            }

            //            if (wb8.IsEnabled)
            //            {
            //                wb8.Navigate(new Uri(textBox1.Text, UriKind.Absolute), null, useragent);
            //            }
            //        }
            //        rectangle1.Visibility = Visibility.Collapsed;
            //    }
        }

        //textBox1 got focus
        private void textBox1_GotFocus(object sender, RoutedEventArgs e)
        {
            textBox1.SelectAll();
            rectangle1.Visibility = Visibility.Visible;
        }

        //textBox1 lost focus
        private void textBox1_LostFocus(object sender, RoutedEventArgs e)
        {
            rectangle1.Visibility = Visibility.Collapsed;
        }

        #region Tabs
        private void tab1_Click(object sender, RoutedEventArgs e)
        {
            wb1.IsEnabled = true;
            wb2.IsEnabled = false;
            wb3.IsEnabled = false;
            wb4.IsEnabled = false;
            wb5.IsEnabled = false;
            wb6.IsEnabled = false;
            wb7.IsEnabled = false;
            wb8.IsEnabled = false;

            wb1.Visibility = Visibility.Visible;
            wb2.Visibility = Visibility.Collapsed;
            wb3.Visibility = Visibility.Collapsed;
            wb4.Visibility = Visibility.Collapsed;
            wb5.Visibility = Visibility.Collapsed;
            wb6.Visibility = Visibility.Collapsed;
            wb7.Visibility = Visibility.Collapsed;
            wb8.Visibility = Visibility.Collapsed;

            try
            {
                Uri uri = new Uri("http://www.google.com/s2/favicons?domain=" + wb1.Source.Host, UriKind.Absolute);
                textBox1.ActionIcon = new System.Windows.Media.Imaging.BitmapImage(uri);

                textBox1.Text = wb1.Source.AbsoluteUri;
            }
            catch { }
        }

        private void tab2_Click(object sender, RoutedEventArgs e)
        {
            wb2.IsEnabled = true;
            wb1.IsEnabled = false;
            wb3.IsEnabled = false;
            wb4.IsEnabled = false;
            wb5.IsEnabled = false;
            wb6.IsEnabled = false;
            wb7.IsEnabled = false;
            wb8.IsEnabled = false;

            wb2.Visibility = Visibility.Visible;
            wb1.Visibility = Visibility.Collapsed;
            wb3.Visibility = Visibility.Collapsed;
            wb4.Visibility = Visibility.Collapsed;
            wb5.Visibility = Visibility.Collapsed;
            wb6.Visibility = Visibility.Collapsed;
            wb7.Visibility = Visibility.Collapsed;
            wb8.Visibility = Visibility.Collapsed;


            try
            {
                Uri uri = new Uri("http://www.google.com/s2/favicons?domain=" + wb2.Source.Host, UriKind.Absolute);
                textBox1.ActionIcon = new System.Windows.Media.Imaging.BitmapImage(uri);

                textBox1.Text = wb2.Source.AbsoluteUri;
            }
            catch { }
        }

        private void tab3_Click(object sender, RoutedEventArgs e)
        {
            wb3.IsEnabled = true;
            wb1.IsEnabled = false;
            wb2.IsEnabled = false;
            wb4.IsEnabled = false;
            wb5.IsEnabled = false;
            wb6.IsEnabled = false;
            wb7.IsEnabled = false;
            wb8.IsEnabled = false;

            wb3.Visibility = Visibility.Visible;
            wb1.Visibility = Visibility.Collapsed;
            wb2.Visibility = Visibility.Collapsed;
            wb4.Visibility = Visibility.Collapsed;
            wb5.Visibility = Visibility.Collapsed;
            wb6.Visibility = Visibility.Collapsed;
            wb7.Visibility = Visibility.Collapsed;
            wb8.Visibility = Visibility.Collapsed;

            try
            {
                Uri uri = new Uri("http://www.google.com/s2/favicons?domain=" + wb3.Source.Host, UriKind.Absolute);
                textBox1.ActionIcon = new System.Windows.Media.Imaging.BitmapImage(uri);

                textBox1.Text = wb3.Source.AbsoluteUri;
            }
            catch { }
        }

        private void tab4_Click(object sender, RoutedEventArgs e)
        {
            wb4.IsEnabled = true;
            wb1.IsEnabled = false;
            wb3.IsEnabled = false;
            wb2.IsEnabled = false;
            wb5.IsEnabled = false;
            wb6.IsEnabled = false;
            wb7.IsEnabled = false;
            wb8.IsEnabled = false;

            wb4.Visibility = Visibility.Visible;
            wb1.Visibility = Visibility.Collapsed;
            wb3.Visibility = Visibility.Collapsed;
            wb2.Visibility = Visibility.Collapsed;
            wb5.Visibility = Visibility.Collapsed;
            wb6.Visibility = Visibility.Collapsed;
            wb7.Visibility = Visibility.Collapsed;
            wb8.Visibility = Visibility.Collapsed;

            try
            {
                Uri uri = new Uri("http://www.google.com/s2/favicons?domain=" + wb4.Source.Host, UriKind.Absolute);
                textBox1.ActionIcon = new System.Windows.Media.Imaging.BitmapImage(uri);

                textBox1.Text = wb4.Source.AbsoluteUri;
            }
            catch { }
        }

        private void tab5_Click(object sender, RoutedEventArgs e)
        {
            wb5.IsEnabled = true;
            wb1.IsEnabled = false;
            wb3.IsEnabled = false;
            wb4.IsEnabled = false;
            wb2.IsEnabled = false;
            wb6.IsEnabled = false;
            wb7.IsEnabled = false;
            wb8.IsEnabled = false;

            wb5.Visibility = Visibility.Visible;
            wb1.Visibility = Visibility.Collapsed;
            wb3.Visibility = Visibility.Collapsed;
            wb4.Visibility = Visibility.Collapsed;
            wb2.Visibility = Visibility.Collapsed;
            wb6.Visibility = Visibility.Collapsed;
            wb7.Visibility = Visibility.Collapsed;
            wb8.Visibility = Visibility.Collapsed;

            try
            {
                Uri uri = new Uri("http://www.google.com/s2/favicons?domain=" + wb5.Source.Host, UriKind.Absolute);
                textBox1.ActionIcon = new System.Windows.Media.Imaging.BitmapImage(uri);

                textBox1.Text = wb5.Source.AbsoluteUri;
            }
            catch { }
        }

        private void tab6_Click(object sender, RoutedEventArgs e)
        {
            wb6.IsEnabled = true;
            wb1.IsEnabled = false;
            wb3.IsEnabled = false;
            wb4.IsEnabled = false;
            wb5.IsEnabled = false;
            wb2.IsEnabled = false;
            wb7.IsEnabled = false;
            wb8.IsEnabled = false;

            wb6.Visibility = Visibility.Visible;
            wb1.Visibility = Visibility.Collapsed;
            wb3.Visibility = Visibility.Collapsed;
            wb4.Visibility = Visibility.Collapsed;
            wb5.Visibility = Visibility.Collapsed;
            wb2.Visibility = Visibility.Collapsed;
            wb7.Visibility = Visibility.Collapsed;
            wb8.Visibility = Visibility.Collapsed;

            try
            {
                Uri uri = new Uri("http://www.google.com/s2/favicons?domain=" + wb6.Source.Host, UriKind.Absolute);
                textBox1.ActionIcon = new System.Windows.Media.Imaging.BitmapImage(uri);

                textBox1.Text = wb6.Source.AbsoluteUri;
            }
            catch { }
        }

        private void tab7_Click(object sender, RoutedEventArgs e)
        {
            wb7.IsEnabled = true;
            wb1.IsEnabled = false;
            wb3.IsEnabled = false;
            wb4.IsEnabled = false;
            wb5.IsEnabled = false;
            wb6.IsEnabled = false;
            wb2.IsEnabled = false;
            wb8.IsEnabled = false;

            wb7.Visibility = Visibility.Visible;
            wb1.Visibility = Visibility.Collapsed;
            wb3.Visibility = Visibility.Collapsed;
            wb4.Visibility = Visibility.Collapsed;
            wb5.Visibility = Visibility.Collapsed;
            wb6.Visibility = Visibility.Collapsed;
            wb2.Visibility = Visibility.Collapsed;
            wb8.Visibility = Visibility.Collapsed;

            try
            {
                Uri uri = new Uri("http://www.google.com/s2/favicons?domain=" + wb7.Source.Host, UriKind.Absolute);
                textBox1.ActionIcon = new System.Windows.Media.Imaging.BitmapImage(uri);

                textBox1.Text = wb7.Source.AbsoluteUri;
            }
            catch { }
        }

        private void tab8_Click(object sender, RoutedEventArgs e)
        {
            wb8.IsEnabled = true;
            wb1.IsEnabled = false;
            wb3.IsEnabled = false;
            wb4.IsEnabled = false;
            wb5.IsEnabled = false;
            wb6.IsEnabled = false;
            wb7.IsEnabled = false;
            wb2.IsEnabled = false;

            wb8.Visibility = Visibility.Visible;
            wb1.Visibility = Visibility.Collapsed;
            wb3.Visibility = Visibility.Collapsed;
            wb4.Visibility = Visibility.Collapsed;
            wb5.Visibility = Visibility.Collapsed;
            wb6.Visibility = Visibility.Collapsed;
            wb7.Visibility = Visibility.Collapsed;
            wb2.Visibility = Visibility.Collapsed;

            try
            {
                Uri uri = new Uri("http://www.google.com/s2/favicons?domain=" + wb8.Source.Host, UriKind.Absolute);
                textBox1.ActionIcon = new System.Windows.Media.Imaging.BitmapImage(uri);

                textBox1.Text = wb8.Source.AbsoluteUri;
            }
            catch { }
        }
        #endregion

        
        //Menu button clicked
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            if (ismenuopen == false)
            {
                OpenMenu.Begin();
                ismenuopen = true;
            }
            else
            {
                CloseMenu.Begin();
                ismenuopen = false;
            }
        }

        //Forward button clicked
        private void button2_Click(object sender, RoutedEventArgs e)
        {
            if (wb1.IsEnabled)
            {
                wb1.InvokeScript("eval", "history.go(1)");
            }

            if (wb2.IsEnabled)
            {
                wb2.InvokeScript("eval", "history.go(1)");
            }

            if (wb3.IsEnabled)
            {
                wb3.InvokeScript("eval", "history.go(1)");
            }

            if (wb4.IsEnabled)
            {
                wb4.InvokeScript("eval", "history.go(1)");
            }

            if (wb5.IsEnabled)
            {
                wb5.InvokeScript("eval", "history.go(1)");
            }

            if (wb6.IsEnabled)
            {
                wb6.InvokeScript("eval", "history.go(1)");
            }

            if (wb7.IsEnabled)
            {
                wb7.InvokeScript("eval", "history.go(1)");
            }

            if (wb8.IsEnabled)
            {
                wb8.InvokeScript("eval", "history.go(1)");
            }
        }

        #region Web browser navigated events
        private void wb1_Navigated(object sender, System.Windows.Navigation.NavigationEventArgs e)
        {
            PB1.IsIndeterminate = false;

            tab1.Content = wb1.Source.AbsoluteUri;

            Uri uri = new Uri("http://www.google.com/s2/favicons?domain=" + wb1.Source.Host, UriKind.Absolute);
            image2.Source = new System.Windows.Media.Imaging.BitmapImage(uri);
            textBox1.ActionIcon = new System.Windows.Media.Imaging.BitmapImage(uri);
            textBox1.Text = wb1.Source.AbsoluteUri;
        }

        private void wb2_Navigated(object sender, System.Windows.Navigation.NavigationEventArgs e)
        {
            PB1.IsIndeterminate = false;

            tab2.Content = wb2.Source.AbsoluteUri;

            Uri uri = new Uri("http://www.google.com/s2/favicons?domain=" + wb2.Source.Host, UriKind.Absolute);
            image3.Source = new System.Windows.Media.Imaging.BitmapImage(uri);
            textBox1.ActionIcon = new System.Windows.Media.Imaging.BitmapImage(uri);
            textBox1.Text = wb2.Source.AbsoluteUri;
        }

        private void wb3_Navigated(object sender, System.Windows.Navigation.NavigationEventArgs e)
        {
            PB1.IsIndeterminate = false;

            tab3.Content = wb3.Source.AbsoluteUri;

            Uri uri = new Uri("http://www.google.com/s2/favicons?domain=" + wb3.Source.Host, UriKind.Absolute);
            image4.Source = new System.Windows.Media.Imaging.BitmapImage(uri);
            textBox1.ActionIcon = new System.Windows.Media.Imaging.BitmapImage(uri);
            textBox1.Text = wb3.Source.AbsoluteUri;
        }

        private void wb4_Navigated(object sender, System.Windows.Navigation.NavigationEventArgs e)
        {
            PB1.IsIndeterminate = false;

            tab4.Content = wb4.Source.AbsoluteUri;

            Uri uri = new Uri("http://www.google.com/s2/favicons?domain=" + wb4.Source.Host, UriKind.Absolute);
            image5.Source = new System.Windows.Media.Imaging.BitmapImage(uri);
            textBox1.ActionIcon = new System.Windows.Media.Imaging.BitmapImage(uri);
            textBox1.Text = wb4.Source.AbsoluteUri;
        }

        private void wb5_Navigated(object sender, System.Windows.Navigation.NavigationEventArgs e)
        {
            PB1.IsIndeterminate = false;

            tab5.Content = wb5.Source.AbsoluteUri;

            Uri uri = new Uri("http://www.google.com/s2/favicons?domain=" + wb5.Source.Host, UriKind.Absolute);
            image6.Source = new System.Windows.Media.Imaging.BitmapImage(uri);
            textBox1.ActionIcon = new System.Windows.Media.Imaging.BitmapImage(uri);
            textBox1.Text = wb5.Source.AbsoluteUri;
        }

        private void wb6_Navigated(object sender, System.Windows.Navigation.NavigationEventArgs e)
        {
            PB1.IsIndeterminate = false;

            tab6.Content = wb6.Source.AbsoluteUri;

            Uri uri = new Uri("http://www.google.com/s2/favicons?domain=" + wb6.Source.Host, UriKind.Absolute);
            image7.Source = new System.Windows.Media.Imaging.BitmapImage(uri);
            textBox1.ActionIcon = new System.Windows.Media.Imaging.BitmapImage(uri);
            textBox1.Text = wb6.Source.AbsoluteUri;
        }

        private void wb7_Navigated(object sender, System.Windows.Navigation.NavigationEventArgs e)
        {
            PB1.IsIndeterminate = false;

            tab7.Content = wb7.Source.AbsoluteUri;

            Uri uri = new Uri("http://www.google.com/s2/favicons?domain=" + wb7.Source.Host, UriKind.Absolute);
            image8.Source = new System.Windows.Media.Imaging.BitmapImage(uri);
            textBox1.ActionIcon = new System.Windows.Media.Imaging.BitmapImage(uri);
            textBox1.Text = wb7.Source.AbsoluteUri;
        }

        private void wb8_Navigated(object sender, System.Windows.Navigation.NavigationEventArgs e)
        {
            PB1.IsIndeterminate = false;

            tab8.Content = wb8.Source.AbsoluteUri;

            Uri uri = new Uri("http://www.google.com/s2/favicons?domain=" + wb8.Source.Host, UriKind.Absolute);
            image9.Source = new System.Windows.Media.Imaging.BitmapImage(uri);
            textBox1.ActionIcon = new System.Windows.Media.Imaging.BitmapImage(uri);
            textBox1.Text = wb8.Source.AbsoluteUri;
        }
        #endregion

        //About button clicked
        private void button5_Click(object sender, RoutedEventArgs e)
        {
            AboutPrompt about = new AboutPrompt();
            about.Footer = @"http://webbit.codeplex.com/";
            about.Body = new TextBlock { Text = "Thank you for using my app!\r\n\r\nFeature suggestions, Rating and reviews:", TextWrapping = TextWrapping.Wrap };
            about.Show();
        }

        //textBox1 icon tapped
        private void textBox1_ActionIconTapped(object sender, EventArgs e)
        {
            if (wb1.IsEnabled)
            {
                wb1.Navigate(new Uri(wb1.Source.AbsoluteUri, UriKind.Absolute), null, useragent);
            }

            if (wb2.IsEnabled)
            {
                wb2.Navigate(new Uri(wb2.Source.AbsoluteUri, UriKind.Absolute), null, useragent);
            }

            if (wb3.IsEnabled)
            {
                wb3.Navigate(new Uri(wb3.Source.AbsoluteUri, UriKind.Absolute), null, useragent);
            }

            if (wb4.IsEnabled)
            {
                wb4.Navigate(new Uri(wb4.Source.AbsoluteUri, UriKind.Absolute), null, useragent);
            }

            if (wb5.IsEnabled)
            {
                wb5.Navigate(new Uri(wb5.Source.AbsoluteUri, UriKind.Absolute), null, useragent);
            }

            if (wb6.IsEnabled)
            {
                wb6.Navigate(new Uri(wb6.Source.AbsoluteUri, UriKind.Absolute), null, useragent);
            }

            if (wb7.IsEnabled)
            {
                wb7.Navigate(new Uri(wb7.Source.AbsoluteUri, UriKind.Absolute), null, useragent);
            }

            if (wb8.IsEnabled)
            {
                wb8.Navigate(new Uri(wb8.Source.AbsoluteUri, UriKind.Absolute), null, useragent);
            }
        }

        //Chrome user agent is set
        private void RadioButton_Checked(object sender, RoutedEventArgs e)
        {
            RB2.IsChecked = false;
            RB3.IsChecked = false;
            useragent = "User-Agent: Mozilla/5.0 (Linux; Android 4.0.4; LG-L160L Build/IML74K) AppleWebkit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Mobile Safari/535.19";

            //MessageBox.Show("Please click the icon on the address bar to apply the changes.");

            UpdateDataKey();
        }

        //Opera user agent is set
        private void RadioButton_Checked_1(object sender, RoutedEventArgs e)
        {
            RB1.IsChecked = false;
            RB3.IsChecked = false;

            useragent = "User-Agent: Opera/12.02 (Android 4.1; Linux; Opera Mobi/ADR-1111101157; U; en-US) Presto/2.9.201 Version/12.02";

            //MessageBox.Show("Please click the icon on the address bar to apply the changes.");
            UpdateDataKey();
        }

        //IE user agent is set
        private void RadioButton_Checked_2(object sender, RoutedEventArgs e)
        {
            RB1.IsChecked = false;
            RB2.IsChecked = false;

            useragent = "User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows Phone OS 7.5; Trident/5.0; IEMobile/9.0)";

            //MessageBox.Show("Please click the icon on the address bar to apply the changes.");

            UpdateDataKey();
        }

        //Back button(Software)
        private void backButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                BackButtonClicked.Begin();
                if (wb1.IsEnabled)
                {
                    wb1.InvokeScript("eval", "history.go(-1)");
                }

                if (wb2.IsEnabled)
                {
                    wb2.InvokeScript("eval", "history.go(-1)");
                }

                if (wb3.IsEnabled)
                {
                    wb3.InvokeScript("eval", "history.go(-1)");
                }

                if (wb4.IsEnabled)
                {
                    wb4.InvokeScript("eval", "history.go(-1)");
                }

                if (wb5.IsEnabled)
                {
                    wb5.InvokeScript("eval", "history.go(-1)");
                }

                if (wb6.IsEnabled)
                {
                    wb6.InvokeScript("eval", "history.go(-1)");
                }

                if (wb7.IsEnabled)
                {
                    wb7.InvokeScript("eval", "history.go(-1)");
                }

                if (wb8.IsEnabled)
                {
                    wb8.InvokeScript("eval", "history.go(-1)");
                }
            }
            catch
            { }
        }

        //Bing picture of the day finished downloading
        //void pun_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        //{
        //    if (e.Error == null && !e.Cancelled)
        //    {
        //        XElement resultElements = XElement.Parse(e.Result);
                
        //            URL = resultElements.Element("image").Element("url").Value;
        //            string fileName = URL;
        //            System.Windows.Media.Imaging.BitmapImage image = new System.Windows.Media.Imaging.BitmapImage(new Uri("http://www.bing.com" + fileName, UriKind.Absolute));
        //            ImageBrush brush = new ImageBrush();
        //            brush.Stretch = Stretch.UniformToFill;
        //            brush.ImageSource = image;
        //            settings.Background = brush;
        //            ContentPanel.Background = brush;
               
        //    }
        //}

        private void CheckBox_Checked(object sender, RoutedEventArgs e)
        {
            UpdateDataKey();
        }

        private void CheckBox_Unchecked(object sender, RoutedEventArgs e)
        {
            UpdateDataKey();
        }

        private void CB1_Checked(object sender, RoutedEventArgs e)
        {
            try
            {
                BrowserHost.Margin = new Thickness(0, 60, 0, 60);
                panorama1.Visibility = Visibility.Visible;

                UpdateDataKey();
            }
            catch { }
        }

        private void CB1_Unchecked(object sender, RoutedEventArgs e)
        {
            try
            {
                BrowserHost.Margin = new Thickness(0, 0, 0, 60);
                panorama1.Visibility = Visibility.Collapsed;

                UpdateDataKey();
            }
            catch { }
        }

        private void ts1checked(object sender, RoutedEventArgs e)
        {
            UpdateDataKey();
        }

        private void ts1unchecked(object sender, RoutedEventArgs e)
        {
            UpdateDataKey();
        }

        bool isbrowserfullscreen = false;
        //User has flicked the webbrowser
        private void OnFlick(object sender, FlickGestureEventArgs e)
        {
            if (e.Direction == System.Windows.Controls.Orientation.Vertical)
            {
                if (isbrowserfullscreen == true)
                {
                    if (CB1.IsChecked == false)
                    {
                        BrowserHost.Margin = new Thickness(0, 0, 0, 60);
                        //BrowserHostHideNoTabs.Begin();

                        //CreateFadeInOutAnimation(1.0, 0.0, panorama1, "Height").Begin();
                        //panorama1.Visibility = Visibility.Collapsed;
                        panorama1hide.Begin();

                        //CreateFadeInOutAnimation(0.0, 1.0, grid1, "Height").Begin();
                        //grid1.Visibility = Visibility.Visible;
                        grid1show.Begin();

                    }
                    else
                    {
                        BrowserHost.Margin = new Thickness(0, 60, 0, 60);
                        //BrowserHostHide.Begin();

                        //panorama1.Visibility = Visibility.Visible;
                        //CreateFadeInOutAnimation(0.0, 1.0, panorama1, "Height").Begin();
                        panorama1show.Begin();

                        //grid1.Visibility = Visibility.Visible;
                        //CreateFadeInOutAnimation(0.0, 1.0, grid1, "Height").Begin();
                        grid1show.Begin();
                    }
                    isbrowserfullscreen = false;
                }
            }
            else
            {
                //To do: Tab switching
            }
        }

        //User has long tapped the webbrowser
        private void OnHold(object sender, Microsoft.Phone.Controls.GestureEventArgs e)
        {
            if (isbrowserfullscreen == false)
            {
                if (CB1.IsChecked == false)
                {
                    BrowserHost.Margin = new Thickness(0, 0, 0, 0);
                    //BrowserHostShow.Begin();

                    //CreateFadeInOutAnimation(1.0, 0.0, panorama1, "Height").Begin();
                    //panorama1.Visibility = Visibility.Collapsed;
                    panorama1hide.Begin();

                    grid1hide.Begin();
                    //CreateFadeInOutAnimation(1.0, 0.0, grid1, "Height").Begin();
                    //grid1.Visibility = Visibility.Collapsed;
                }
                else
                {
                    BrowserHost.Margin = new Thickness(0, 0, 0, 0);
                    //BrowserHostShow.Begin();

                    //CreateFadeInOutAnimation(1.0, 0.0, panorama1, "Height").Begin();
                    //panorama1.Visibility = Visibility.Collapsed;
                    panorama1hide.Begin();

                    //CreateFadeInOutAnimation(1.0, 0.0, grid1, "Height").Begin();
                    //grid1.Visibility = Visibility.Collapsed;
                    grid1hide.Begin();
                }
                isbrowserfullscreen = true;
            }
        }

        //Help > Finish button clicked
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            IntroScreen.Visibility = Visibility.Collapsed;
        }

        //Help button clicked
        private void buttonHelp_Click(object sender, RoutedEventArgs e)
        {
            IntroScreen.Visibility = Visibility.Visible;
        }

        //Weather information downloaded
        void webclient2_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                MessageBox.Show("error");

            }

            XElement XmlWeather = XElement.Parse(e.Result);
            //current
            //listBoxc.ItemsSource = from weather in XmlWeather.Descendants("current_condition")

            //                       select new WeatherInfoCurrent
            //                       {

            //                           observation_time = weather.Element("observation_time").Value,
            //                           temp_C = weather.Element("temp_C").Value,
            //                           temp_F = weather.Element("temp_F").Value,
            //                           icon = weather.Element("weatherIconUrl").Value,
            //                           condition = weather.Element("weatherDesc").Value
            //                       };


            //all three days
            listBox1.ItemsSource = from weather in XmlWeather.Descendants("weather")

                                   select new WeatherInfo
                                   {

                                       day_of_week = weather.Element("date").Value,
                                       high = weather.Element("tempMaxF").Value,
                                       low = weather.Element("tempMinF").Value,
                                       icon = weather.Element("weatherIconUrl").Value,
                                       condition = weather.Element("weatherDesc").Value
                                   };


        }

        //textBox2 icon tapped
        private void textBox2_ActionIconTapped(object sender, EventArgs e)
        {
            WebClient webclient = new WebClient();
            webclient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(webclient2_DownloadStringCompleted);

            webclient.DownloadStringAsync(new Uri("http://free.worldweatheronline.com/feed/weather.ashx?key=197e356c12132853131101&q=" + textBox2.Text + "&num_of_days=3&format=xml")); // weather location 
            //http://free.worldweatheronline.com/feed/weather.ashx?key=197e356c12132853131101&q=mumbai,india&num_of_days=7&format=xml

            //CurrentWeather.Text = textBox2.Text + " Current Condition";

            IsolatedStorageSettings ISSetting = IsolatedStorageSettings.ApplicationSettings;

            if (!ISSetting.Contains("Weather"))
            {
                ISSetting.Add("Weather", textBox2.Text);
            }
            else
            {
                ISSetting["Weather"] = textBox2.Text;
            }
            ISSetting.Save();
        }

        //textBox2 key pressed
        private void textBox2_KeyDown(object sender, KeyEventArgs e)
        {
            WebClient webclient = new WebClient();
            webclient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(webclient2_DownloadStringCompleted);

            webclient.DownloadStringAsync(new Uri("http://free.worldweatheronline.com/feed/weather.ashx?key=197e356c12132853131101&q=" + textBox2.Text + "&num_of_days=3&format=xml")); // weather location 
            //http://free.worldweatheronline.com/feed/weather.ashx?key=197e356c12132853131101&q=mumbai,india&num_of_days=7&format=xml

            //CurrentWeather.Text = textBox2.Text + " Current Condition";

            IsolatedStorageSettings ISSetting = IsolatedStorageSettings.ApplicationSettings;

            if (!ISSetting.Contains("Weather"))
            {
                ISSetting.Add("Weather", textBox2.Text);
            }
            else
            {
                ISSetting["Weather"] = textBox2.Text;
            }
            ISSetting.Save();
        }

        #region Facebook
        //Facebook button clicked
        private void Facebook_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/Pages/FacebookLoginPage.xaml", UriKind.Relative));
        }

        private void loadinformationclicked(object sender, RoutedEventArgs e)
        {
            loadingGrid.Visibility = Visibility.Visible;

            var loginUrl = GetFacebookLoginUrl(AppId, ExtendedPermissions);
            authorizationBrowser.Navigate(loginUrl);

            loadingGrid.Visibility = Visibility.Collapsed;
            authorizationBrowser.Visibility = Visibility.Visible;
        }

        private Uri GetFacebookLoginUrl(string appId, string extendedPermissions)
        {
            var parameters = new Dictionary<string, object>();
            parameters["client_id"] = appId;
            parameters["redirect_uri"] = "https://www.facebook.com/connect/login_success.html";
            parameters["response_type"] = "token";
            parameters["display"] = "touch";

            // add the 'scope' only if we have extendedPermissions.
            if (!string.IsNullOrEmpty(extendedPermissions))
            {
                // A comma-delimited list of permissions
                parameters["scope"] = extendedPermissions;
            }

            return _fb.GetLoginUrl(parameters);
        }

        private void authorizationBrowser_Navigated(object sender, System.Windows.Navigation.NavigationEventArgs e)
        {
            FacebookOAuthResult oauthResult;
            if (!_fb.TryParseOAuthCallbackUrl(e.Uri, out oauthResult))
            {
                return;
            }

            if (oauthResult.IsSuccess)
            {
                var accessToken = oauthResult.AccessToken;
                LoginSucceded(accessToken);
            }
            else
            {
                // user cancelled
                MessageBox.Show(oauthResult.ErrorDescription);
            }
        }

        private void LoginSucceded(string accessToken)
        {

            authorizationBrowser.Visibility = Visibility.Collapsed;

            var fb = new FacebookClient(accessToken);

            fb.GetCompleted += (o, e) =>
            {
                if (e.Error != null)
                {
                    Dispatcher.BeginInvoke(() => MessageBox.Show(e.Error.Message));
                    return;
                }

                var result = (IDictionary<string, object>)e.GetResultData();
                var id = (string)result["id"];

                //var url = string.Format("/Pages/FacebookInfoPage.xaml?access_token={0}&id={1}", accessToken, id);

                //Dispatcher.BeginInvoke(() => NavigationService.Navigate(new Uri(url, UriKind.Relative)));

                _accessToken = accessToken;
                _userId = id;

                LoadFacebookData();
            };

            fb.GetAsync("me?fields=id");
        }

        private void LoadFacebookData()
        {
            GetUserProfilePicture();

            GraphApiSample();

            FqlSample();
        }

        private void GetUserProfilePicture()
        {
            // available picture types: square (50x50), small (50xvariable height), large (about 200x variable height) (all size in pixels)
            // for more info visit http://developers.facebook.com/docs/reference/api
            string profilePictureUrl = string.Format("https://graph.facebook.com/{0}/picture?type={1}&access_token={2}", _userId, "square", _accessToken);

            //picProfile.Source = new BitmapImage(new Uri(profilePictureUrl));
        }

        private void GraphApiSample()
        {
            var fb = new FacebookClient(_accessToken);

            fb.GetCompleted += (o, e) =>
            {
                if (e.Error != null)
                {
                    Dispatcher.BeginInvoke(() => MessageBox.Show(e.Error.Message));
                    return;
                }

                var result = (IDictionary<string, object>)e.GetResultData();

                Dispatcher.BeginInvoke(() =>
                {
                    ProfileName.Text = "Hi " + (string)result["name"];
                    FirstName.Text = "First Name: " + (string)result["first_name"];
                    FirstName.Text = "Last Name: " + (string)result["last_name"];
                });
            };

            fb.GetAsync("me");
        }

        private void FqlSample()
        {
            var fb = new FacebookClient(_accessToken);

            fb.GetCompleted += (o, e) =>
            {
                if (e.Error != null)
                {
                    Dispatcher.BeginInvoke(() => MessageBox.Show(e.Error.Message));
                    return;
                }

                var result = (IDictionary<string, object>)e.GetResultData();
                var data = (IList<object>)result["data"];

                var count = data.Count;

                // since this is an async callback, make sure to be on the right thread
                // when working with the UI.
                Dispatcher.BeginInvoke(() =>
                {
                    TotalFriends.Text = string.Format("You have {0} friend(s).", count);
                });
            };

            // query to get all the friends
            var query = string.Format("SELECT uid,pic_square FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1={0})", "me()");

            // Note: For windows phone 7, make sure to add [assembly: InternalsVisibleTo("Facebook")] if you are using anonymous objects as parameter.
            fb.GetAsync("fql", new { q = query });
        }

        private string _lastMessageId;
        private void PostToWall_Click(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(txtMessage.Text))
            {
                MessageBox.Show("Enter message.");
                return;
            }

            var fb = new FacebookClient(_accessToken);

            fb.PostCompleted += (o, args) =>
            {
                if (args.Error != null)
                {
                    Dispatcher.BeginInvoke(() => MessageBox.Show(args.Error.Message));
                    return;
                }

                var result = (IDictionary<string, object>)args.GetResultData();
                _lastMessageId = (string)result["id"];

                Dispatcher.BeginInvoke(() =>
                {
                    MessageBox.Show("Message Posted successfully");

                    txtMessage.Text = string.Empty;
                    btnDeleteLastMessage.IsEnabled = true;
                });
            };

            var parameters = new Dictionary<string, object>();
            parameters["message"] = txtMessage.Text;

            fb.PostAsync("me/feed", parameters);
        }

        private void DeleteLastMessage_Click(object sender, RoutedEventArgs e)
        {
            btnDeleteLastMessage.IsEnabled = false;

            var fb = new FacebookClient(_accessToken);

            fb.DeleteCompleted += (o, args) =>
            {
                if (args.Error != null)
                {
                    Dispatcher.BeginInvoke(() => MessageBox.Show(args.Error.Message));
                    return;
                }

                Dispatcher.BeginInvoke(() =>
                {
                    MessageBox.Show("Message deleted successfully");
                    btnDeleteLastMessage.IsEnabled = false;
                });
            };

            fb.DeleteAsync(_lastMessageId);
        }
        #endregion

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            textBox1.Text = NavigationContext.QueryString["SearchBoxText"];

            if (textBox1.Text.Contains(" ") || !textBox1.Text.Contains("."))
            {
                //Search Google

                string String = textBox1.Text.Replace(" ", "+");
                wb1.Navigate(new Uri("http://www.google.com/search?&q=" + String, UriKind.Absolute), null, useragent);
            }
            else if (textBox1.Text.StartsWith("http://") || textBox1.Text.StartsWith("www.") || textBox1.Text.Contains("."))
            {
                //Navigate with absolute uri

                try
                {
                    wb1.Navigate(new Uri(textBox1.Text, UriKind.Absolute), null, useragent);
                }
                catch
                {
                    try
                    {
                        textBox1.Text = "http://" + textBox1.Text;
                        wb1.Navigate(new Uri(textBox1.Text, UriKind.Absolute), null, useragent);
                    }
                    catch
                    {
                        MessageBox.Show("Invalid URL");
                    }
                }
            }


            //Weather
            if (IsolatedStorageSettings.ApplicationSettings.Contains("Weather"))
            {
                string weather = IsolatedStorageSettings.ApplicationSettings["Weather"] as string;
                string datakey = IsolatedStorageSettings.ApplicationSettings["DataKey"] as string;

                if (datakey.Contains("ts1ischecked"))
                {
                    textBox2.Text = weather;
                    TS1.IsChecked = true;
                    textBox2_KeyDown(null, null);
                }
                else
                {
                    TS1.IsChecked = false;
                }

            }

            
        }

        private void MenuItem_Refresh_Click(object sender, RoutedEventArgs e)
        {
            if (wb1.IsEnabled)
            {
                wb1.Navigate(new Uri(wb1.Source.AbsoluteUri, UriKind.Absolute), null, useragent);
            }

            if (wb2.IsEnabled)
            {
                wb2.Navigate(new Uri(wb2.Source.AbsoluteUri, UriKind.Absolute), null, useragent);
            }

            if (wb3.IsEnabled)
            {
                wb3.Navigate(new Uri(wb3.Source.AbsoluteUri, UriKind.Absolute), null, useragent);
            }

            if (wb4.IsEnabled)
            {
                wb4.Navigate(new Uri(wb4.Source.AbsoluteUri, UriKind.Absolute), null, useragent);
            }

            if (wb5.IsEnabled)
            {
                wb5.Navigate(new Uri(wb5.Source.AbsoluteUri, UriKind.Absolute), null, useragent);
            }

            if (wb6.IsEnabled)
            {
                wb6.Navigate(new Uri(wb6.Source.AbsoluteUri, UriKind.Absolute), null, useragent);
            }

            if (wb7.IsEnabled)
            {
                wb7.Navigate(new Uri(wb7.Source.AbsoluteUri, UriKind.Absolute), null, useragent);
            }

            if (wb8.IsEnabled)
            {
                wb8.Navigate(new Uri(wb8.Source.AbsoluteUri, UriKind.Absolute), null, useragent);
            }
        }

        private void MenuItem_Forward_Click(object sender, RoutedEventArgs e)
        {
            if (wb1.IsEnabled)
            {
                wb1.InvokeScript("eval", "history.go(1)");
            }

            if (wb2.IsEnabled)
            {
                wb2.InvokeScript("eval", "history.go(1)");
            }

            if (wb3.IsEnabled)
            {
                wb3.InvokeScript("eval", "history.go(1)");
            }

            if (wb4.IsEnabled)
            {
                wb4.InvokeScript("eval", "history.go(1)");
            }

            if (wb5.IsEnabled)
            {
                wb5.InvokeScript("eval", "history.go(1)");
            }

            if (wb6.IsEnabled)
            {
                wb6.InvokeScript("eval", "history.go(1)");
            }

            if (wb7.IsEnabled)
            {
                wb7.InvokeScript("eval", "history.go(1)");
            }

            if (wb8.IsEnabled)
            {
                wb8.InvokeScript("eval", "history.go(1)");
            }
        }

        private void MenuItem_Exit_Click(object sender, RoutedEventArgs e)
        {
            //Crash the app
            throw new ExitException();
        }
    }

    #region Weather classes
    public class WeatherInfo
    {
        public string day_of_week { get; set; }
        public string low { get; set; }
        public string high { get; set; }
        public string icon { get; set; }
        public string condition { get; set; }
    }

    public class WeatherInfoCurrent
    {
        public string observation_time { get; set; }
        public string temp_C { get; set; }
        public string temp_F { get; set; }
        public string icon { get; set; }
        public string condition { get; set; }
    }
    #endregion
}